import { Component, OnInit } from '@angular/core';
import { Router } from "@angular/router";
import {Oplists, Question} from './../model/question';
import { AnswerInfo } from './../model/answer';
import { QuestionService } from './../service/question.service';


@Component({
  selector: 'app-question',
  templateUrl: './question.component.html',
  styleUrls: ['./question.component.css']
})
export class QuestionComponent implements OnInit {
  public paperId: string;
  public qlist: Question[];
  public question: Question = new Question();
  public type: string;
  public status = false;

  public index: number = 0;
  public num: number;
  public userId: string = '';
  public username: string = '';
  public score: string;
  public isPass: boolean;

  public answerInfo: AnswerInfo[] = [];
  public answer: AnswerInfo = new AnswerInfo();
  public oneanswer: string[] = [];

  constructor(private questionService: QuestionService, private router: Router) {}

  ngOnInit() {
    this.index = 0;
    this.userId = localStorage.getItem('userId');
    this.username = localStorage.getItem('username');
    this.questionService.questionList(this.userId).subscribe(result => {
      if (result.result === '00000000') {
        this.paperId = result.data.paperId;
        this.qlist = result.data.qlist;
        this.question = result.data.qlist[this.index];
        this.type = result.data.qlist[this.index].type;
        this.num = result.data.qlist.length - 1;
      }
    });
  }
  saveResult() {
    for (let i = 0; i < this.question.oplist.length; i++) {
      if (this.question.oplist[i].clours) {
        this.oneanswer.push(this.question.oplist[i].key);
      }
    }
  }
  nextQuestion() {
    if (this.finish()) {
      this.answer.qid = this.question.qid;
      this.saveResult();
      this.answer.answerlist = this.oneanswer;
      this.answerInfo.push(this.answer);

      this.questionService.commitPaper(this.userId, this.paperId, this.answerInfo).subscribe(result => {
        if (result.result === '00000000') {
          this.score = result.data.score;
          this.isPass = result.data.pass;
          this.status = true;
        }else {
          alert(result.msg);
        }
      });
      // this.router.navigateByUrl('result');
    }else {
      this.answer.qid = this.question.qid;
      this.saveResult();
      this.answer.answerlist = this.oneanswer;
      this.answerInfo.push(this.answer);
      this.index++;
      this.question = this.qlist[this.index];
      this.type = this.qlist[this.index].type;

      this.oneanswer = [];
      this.answer = new AnswerInfo;
    }
  }

  finish() {
    return (this.num === this.index) ? true : false;
  }

  topic_row(item, items) {
    if (this.type === '0') {
      for (let i = 0; i < items.length; i++) {
        if (item.key === items[i].key) {
          this.question.oplist[i].clours = true;
        }else {
          this.question.oplist[i].clours = false;
        }
      }
    }else if (this.type === '1') {
      for (let i = 0; i < items.length; i++) {
        if (item.key === items[i].key) {
          this.question.oplist[i].clours = !item.clours;
        }
      }
    }
  }
}
